//
//  ProgressViewController.swift
//  swift各类控件集锦
//
//  Created by iosdev1 on 2016/12/23.
//  Copyright © 2016年 zyy. All rights reserved.
//

import UIKit

class ProgressViewController: ZyBaseViewController {

    var timer:Timer? = nil
    var i:Float? = 0.0
    
    
    var progressView:UIProgressView? = nil
    
    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
//        1.创建,这里的高度是固定的，设置多少无所谓，只能用下面的CGAffineTransform来更改高度（旋转，缩放等等都可以）
        progressView = UIProgressView.init(frame: CGRect(x:10, y:270, width:120, height:30))
//        2. 属性设置风格：
        //这个后面的“；”如果不写，后面的智能提示会失效
        progressView!.progressViewStyle = UIProgressViewStyle.default;//可以自己看一下效果
        self.view.addSubview(progressView!)
        //设置进度默认值，这个相当于百分比，范围在0~1之间，不可以设置最大最小值
        self.progressView?.progress = 0.8
        //设置进度条上进度的颜色
        self.progressView?.progressTintColor = UIColor.yellow
        self.progressView!.trackTintColor = UIColor.red
        
//        //设置进度条的背景图片,可以间接改变背景色
//        self.progressView?.trackImage = UIImage.init(named: "girl")
//        //设置进度条上进度的背景图片同上
//        self.progressView!.progressImage = UIImage.init(named: "girl")
        
        self.progressView?.addObserver(self, forKeyPath: "progress", options: NSKeyValueObservingOptions.new, context: nil)

        //        CGAffineTransformMake(a,b,c,d,tx,ty)
        //        ad缩放bc旋转tx,ty位移，基础的2D矩阵
//        let transform:CGAffineTransform = __CGAffineTransformMake(<#T##a: CGFloat##CGFloat#>, <#T##b: CGFloat##CGFloat#>, <#T##c: CGFloat##CGFloat#>, <#T##d: CGFloat##CGFloat#>, <#T##tx: CGFloat##CGFloat#>, <#T##ty: CGFloat##CGFloat#>)
        let transform:CGAffineTransform = __CGAffineTransformMake(1, 3.14/4.0, 0, 8, 0, 0)
        self.progressView?.transform = transform

        print("\(UIDevice.current.systemVersion)")
        timer = Timer.scheduledTimer(timeInterval: 0.1, target: self, selector: #selector(timerClick), userInfo: "timer", repeats: true)
    }
    
    func timerClick() {
        UIView.animate(withDuration: 2, animations: {
            self.progressView?.progress = self.i!;
            self.i = self.i!+0.01;
        }) { (true) in
            if self.i == 1 {
             self.progressView?.removeFromSuperview()
            }
        }

//        self.timer?.fireDate = NSDate.distantPast;//打开定时器
//        self.timer?.isValid//定时器是否开启

//        停止计时器并移除
//        timer?.fireDate = NSDate.distantFuture
//        timer?.invalidate()
//        timer = nil
    }

    override func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey : Any]?, context: UnsafeMutableRawPointer?) {
        if keyPath == "progress" {
//            let newprogress = change!.index(forKey: NSKeyValueChangeKey.newKey)
            let newprogress = change?[NSKeyValueChangeKey.newKey] as! CGFloat
            print("\(String(describing: newprogress))")
            if newprogress == 1.0 {
                zyUIshowMessage(message: "结束")
                self.progressView?.removeObserver(self, forKeyPath: "progress")
            }
        }
    }
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    

    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        // Get the new view controller using segue.destinationViewController.
        // Pass the selected object to the new view controller.
    }
    */

}
